﻿/*
Tibia Multi Bot .NET
Copyright (c) 2011 Jakub "Cubix651" Cisło - http://cubix.one.pl

License: X11 (MIT) See README file.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ProcessMemoryReaderLib;

namespace NoLogout
{
    delegate bool HotkeysMethod(int i, int j);

    struct Hotkeys
    {
        public bool isChecked;
        public string Send;
        public int Variable;
        public int Value;
        public HotkeysMethod Method;

        public Hotkeys(string send, int variable, int value, HotkeysMethod method)
        {
            Send = send;
            Variable = variable;
            Value = value;
            Method = method;
            isChecked = true;
        }

        public Hotkeys(string send)
        {
            Send = send;
            Variable = 0;
            Value = 0;
            Method = null;
            isChecked = false;
        }
    }

    public partial class Form1 : Form
    {
        IWshRuntimeLibrary.WshShell shell;
        List<Timer> timers;
        bool right = true, right2 = true;
        //tibia toniego
        int[] Variables = new int[] {0x00635EF0, 0x00635F0C};
        //Tibia 8.10
        //int[] Variables = new int[] { 0xCCCCCC, 0x00613B6C };
        ProcessMemoryReaderLib.ProcessMemoryReader preader;

        object ob = (object)1;
        object ob2 = (object)false;
        object ob3 = (object)"Tibia";

        public Form1()
        {
            InitializeComponent();

            shell = new IWshRuntimeLibrary.WshShell();
            timers = new List<Timer>();
       }

        private int getVariable(int variable)
        {
            int bytesread;
            byte[] memory = preader.ReadProcessMemory((IntPtr)variable, 4, out bytesread);
            return BitConverter.ToInt32(memory, 0);
        }

        private bool isEqual(int i, int j)
        {
            return i == j;
        }

        private bool isSmaller(int i, int j)
        {
            return i < j;
        }

        private bool isGreater(int i, int j)
        {
            return i > j;
        }

        private bool isSmallerOrEqual(int i, int j)
        {
            return i <= j;
        }

        private bool isGreaterOrEqual(int i, int j)
        {
            return i >= j;
        }

        private void runClient_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(clientPath.Text))
            {
                if (string.IsNullOrEmpty(clientName.Text))
                {
                    MessageBox.Show("Musisz wypełnić pole nazwy!");
                    return;
                }
                ob3 = (object)clientName.Text;
                shell.CurrentDirectory = Directory.GetParent(clientPath.Text).ToString();
                shell.Run("\"" + clientPath.Text + "\"", ref ob, ref ob2);
                if (!string.IsNullOrEmpty(accountNumber.Text) && !string.IsNullOrEmpty(accountPassword.Text))
                {
                    MessageBox.Show("Naciśnij ENTER GAME");
                    System.Threading.Thread.Sleep(5000);
                    shell.AppActivate(ref ob3, ref ob2);
                    shell.SendKeys(accountNumber.Text, ref ob2);
                    shell.SendKeys("{TAB}", ref ob2);
                    shell.SendKeys(accountPassword.Text, ref ob2);
                    shell.SendKeys("{ENTER}", ref ob2);
                    System.Threading.Thread.Sleep(3000);
                    shell.AppActivate(ref ob3, ref ob2);
                    shell.SendKeys("{ENTER}", ref ob2);
                }
            }
            else MessageBox.Show("Jeżeli chcesz uruchomić klienta, najpierw podaj jego ścieżkę!");
        }

        private void startBot_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(clientName.Text))
            {
                MessageBox.Show("Musisz wypełnić pole nazwy!");
                return;
            }
            ob3 = (object)clientName.Text;

            System.Diagnostics.Process[] myprocess = System.Diagnostics.Process.GetProcessesByName(clientName.Text);
            preader = new ProcessMemoryReaderLib.ProcessMemoryReader();
            preader.ReadProcess = myprocess[0];
            preader.OpenProcess();

            stopBot.Enabled = true;
            startBot.Enabled = false;
            stopToolStripMenuItem.Enabled = true;
            startToolStripMenuItem.Enabled = false;
            for (int i = 0; i < timers.Count; i++)
                if (eventsQueue.Items[i].SubItems[2].Text == "On") timers[i].Enabled = true;
        }

        private void stopBot_Click(object sender, EventArgs e)
        {
            foreach (Timer timer in timers)
                timer.Enabled = false;
            startBot.Enabled = true;
            stopBot.Enabled = false;
            stopToolStripMenuItem.Enabled = false;
            startToolStripMenuItem.Enabled = true;
        }

        private void minimalizeApp_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            notifyIcon1.Visible = true;
        }

        private void show_Click(object sender, EventArgs e)
        {
            this.Visible = true;
            notifyIcon1.Visible = false;
        }

        private void exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                show_Click(null, null);
        }

        private void comboBox_TextChanged(object sender, EventArgs e)
        {
            ((ComboBox)sender).Text = "";
        }

        private void choseTibiaClientName_Click(object sender, EventArgs e)
        {
            clientName.Text = "Tibia";
        }

        private void chosePOClientName_Click(object sender, EventArgs e)
        {
            clientName.Text = "PO";
        }

        private void choseClientPath_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Aplikacje|*.exe";
            if (ofd.ShowDialog() == DialogResult.OK) clientPath.Text = ofd.FileName; 
        }

        private void addRotation_Click(object sender, EventArgs e)
        {
            eventsQueue.Items.Add(new ListViewItem(new string[] { "Obróć się", noKickInterval.Value.ToString(), "On" }));
            timers.Add(new Timer());
            timers[timers.Count - 1].Interval = (int)noKickInterval.Value;
            timers[timers.Count - 1].Enabled = stopBot.Enabled;
            timers[timers.Count - 1].Tick += new EventHandler(rotation_Tick);
        }

        private void addChangePosition_Click(object sender, EventArgs e)
        {
            eventsQueue.Items.Add(new ListViewItem(new string[] { "Porusz się", noKickInterval.Value.ToString(), "On" }));
            timers.Add(new Timer());
            timers[timers.Count - 1].Interval = (int)noKickInterval.Value;
            timers[timers.Count - 1].Enabled = stopBot.Enabled;
            timers[timers.Count - 1].Tick += new EventHandler(changePosition_Tick);
        }

        private void addHotkey_Click(object sender, EventArgs e)
        {
            if (hotkey.SelectedIndex < 0)
            {
                MessageBox.Show("Musisz wybrać przycisk!");
                return;
            }
            if (ifCheckBox.Checked && ((variables.SelectedIndex==-1) || (operators.SelectedIndex==-1)))
            {
                MessageBox.Show("Musisz wybrać rodzaj punktów i operator!");
                return;
            }
            timers.Add(new Timer());
            timers[timers.Count - 1].Interval = (int)hotkeyInterval.Value;
            timers[timers.Count - 1].Enabled = stopBot.Enabled;
            if (ifCheckBox.Checked)
            {
                eventsQueue.Items.Add(new ListViewItem(new string[] { "Hotkey " + (useCtrl.Checked ? "^" : "")
                            + (useShift.Checked ? "+" : "") + "{" + hotkey.Text + "} jeśli " + variables.SelectedItem.ToString() + operators.SelectedItem.ToString() + variablesPoints.Value, hotkeyInterval.Value.ToString(), "On" }));
                switch (operators.SelectedIndex)
                {
                    case 0:
                        timers[timers.Count - 1].Tag = new Hotkeys((useCtrl.Checked ? "^" : "") + (useShift.Checked ? "+" : "") + "{" + hotkey.Text + "}", Variables[variables.SelectedIndex], (int)variablesPoints.Value, new HotkeysMethod(isGreater));
                        break;
                    case 1:
                        timers[timers.Count - 1].Tag = new Hotkeys((useCtrl.Checked ? "^" : "") + (useShift.Checked ? "+" : "") + "{" + hotkey.Text + "}", Variables[variables.SelectedIndex], (int)variablesPoints.Value, new HotkeysMethod(isSmaller));
                        break;
                    case 2:
                        timers[timers.Count - 1].Tag = new Hotkeys((useCtrl.Checked ? "^" : "") + (useShift.Checked ? "+" : "") + "{" + hotkey.Text + "}", Variables[variables.SelectedIndex], (int)variablesPoints.Value, new HotkeysMethod(isGreaterOrEqual));
                        break;
                    case 3:
                        timers[timers.Count - 1].Tag = new Hotkeys((useCtrl.Checked ? "^" : "") + (useShift.Checked ? "+" : "") + "{" + hotkey.Text + "}", Variables[variables.SelectedIndex], (int)variablesPoints.Value, new HotkeysMethod(isSmallerOrEqual));
                        break;
                    case 4:
                        timers[timers.Count - 1].Tag = new Hotkeys((useCtrl.Checked ? "^" : "") + (useShift.Checked ? "+" : "") + "{" + hotkey.Text + "}", Variables[variables.SelectedIndex], (int)variablesPoints.Value, new HotkeysMethod(isEqual));
                        break;
                }
            }
            else
            {
                eventsQueue.Items.Add(new ListViewItem(new string[] { "Hotkey " + (useCtrl.Checked ? "^" : "")
                    + (useShift.Checked ? "+" : "") + "{" + hotkey.Text + "}", hotkeyInterval.Value.ToString(), "On" }));
                timers[timers.Count - 1].Tag = new Hotkeys((useCtrl.Checked ? "^" : "") + (useShift.Checked ? "+" : "") + "{" + hotkey.Text + "}");
            }
            timers[timers.Count - 1].Tick += new EventHandler(hotkey_Tick);
        }

        private void rotation_Tick(object sender, EventArgs e)
        {
            shell.AppActivate(ref ob3, ref ob2);
            if (right2)
                shell.SendKeys("^{RIGHT}", ref ob2);
            else
                shell.SendKeys("^{LEFT}", ref ob2);
            right2 = !right2;
        }

        private void changePosition_Tick(object sender, EventArgs e)
        {
            shell.AppActivate(ref ob3, ref ob2);
            if (right)
                shell.SendKeys("{RIGHT}", ref ob2);
            else
                shell.SendKeys("{LEFT}", ref ob2);
            right = !right;
        }

        private void hotkey_Tick(object sender, EventArgs e)
        {
            if (!((Hotkeys)((Timer)sender).Tag).isChecked
                || ((Hotkeys)((Timer)sender).Tag).Method(getVariable(((Hotkeys)((Timer)sender).Tag).Variable), ((Hotkeys)((Timer)sender).Tag).Value))
            {
                shell.AppActivate(ref ob3, ref ob2);
                shell.SendKeys(((Hotkeys)((Timer)sender).Tag).Send, ref ob2);
            }
        }

        private void turnOn_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < eventsQueue.SelectedItems.Count; i++)
            {
                eventsQueue.SelectedItems[i].SubItems[2].Text = "On";
                timers[eventsQueue.SelectedItems[i].Index].Enabled = stopBot.Enabled;
            }
        }

        private void turnOff_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < eventsQueue.SelectedItems.Count; i++)
            {
                eventsQueue.SelectedItems[i].SubItems[2].Text = "Off";
                timers[eventsQueue.SelectedItems[i].Index].Enabled = false;
            }
        }

        private void removeItem_Click(object sender, EventArgs e)
        {
            int count = eventsQueue.SelectedItems.Count;
            for (int i = 0; i < count; i++)
            {
                timers[eventsQueue.SelectedItems[0].Index].Enabled = false;
                timers.Remove(timers[eventsQueue.SelectedItems[0].Index]);
                eventsQueue.Items.RemoveAt(eventsQueue.SelectedItems[0].Index);
            }
        }

        private void linkHomePage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://cubix.to.pl/");
        }

        private void ifCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            variables.Enabled = operators.Enabled = variablesPoints.Enabled = ifCheckBox.Checked;
        }
    }
}
